//
//  ParallaxPageViewController.m
//  ActiveDeck
//
//  Created by Laura McBride on 11/1/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "ParallaxPageViewController.h"
#import "AD_ParallaxView.h"
#import "AD_Button.h"
#import "AD_Image.h"
#import "AD_ViewPort_Button_Link.h"

@implementation ParallaxPageViewController
@synthesize ImageBG,PLBTLinkSets;

/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    PLBTLinkSets = [[NSMutableArray alloc]init];
    
    
    [super viewDidLoad];
}

- (void)AddButton:(int)Index_VP
          scrollV:(UIScrollView *)scrollV
{
	
    int i=0;
    
	for(AD_Button *multiButton in [[pageOB.mut_Arr_ParallaxView objectAtIndex:Index_VP] AD_ButtonSets])
	{
        AD_ViewPort_Button_Link *VBLink = [AD_ViewPort_Button_Link alloc];
        VBLink.Index_VP = Index_VP;
        VBLink.Index_BT = i++;
        //NSLog(@"Button Index %d, ViewPort Index %d", VBLink.Index_BT, VBLink.Index_VP);
        
        [PLBTLinkSets addObject:VBLink];
        [VBLink release];
        
		UIImage *offImage = [UIImage imageWithData:[NSData dataWithContentsOfFile:multiButton.imageOff]];
		UIImage *onImage = [UIImage imageWithData:[NSData dataWithContentsOfFile:multiButton.imageOn]];
		CGRect buttonFrame = CGRectMake(multiButton.left, multiButton.top, multiButton.width, multiButton.height);
		NSArray *condition = [multiButton.Condition componentsSeparatedByString:@"=="];
        
		UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [newButton setTag:multiButton.Movement];
		[newButton setFrame:buttonFrame];
		[newButton setTag:[PLBTLinkSets count]-1];
		[newButton setBackgroundImage:offImage forState:UIControlStateNormal];
		[newButton setBackgroundImage:onImage forState:UIControlStateHighlighted];
		[newButton setBackgroundImage:onImage forState:UIControlStateSelected];
		[newButton addTarget:self action:@selector(viewPortbuttonPress:) forControlEvents:UIControlEventTouchUpInside];
		
        [newButton setHidden:[self setCondition:condition]];
		[scrollV addSubview:newButton]; 
	}
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    for (id obj in scrollView.subviews)
    {
        if([obj isKindOfClass:[UIButton class]])
        {
            UIButton *button = (UIButton*) obj;
        
            double displacementX = scrollView.contentOffset.x * button.tag /100;
            double displacementY = scrollView.contentOffset.y * button.tag /100;
            button.frame = CGRectMake(button.frame.origin.x - displacementX, button.frame.origin.y - displacementY, button.frame.size.width, button.frame.size.height);
            
        }else
        {
            UIImageView *img = (UIImageView*)obj;
            
            double displacementX = scrollView.contentOffset.x * img.tag /100;
            double displacementY = scrollView.contentOffset.y * img.tag /100;
            img.frame = CGRectMake(img.frame.origin.x - displacementX, img.frame.origin.y - displacementY, img.frame.size.width, img.frame.size.height);
        }
    }
}


-(void)viewPortbuttonPress:(id)sender
{
	UIButton *pressButton = (UIButton *)sender;
    NSLog(@"ButtonLog%d", pressButton.tag);
    AD_ViewPort_Button_Link * VBLink = (AD_ViewPort_Button_Link*)[PLBTLinkSets objectAtIndex:pressButton.tag];
    NSLog(@"Press Button Index %d, ViewPort Index %d", VBLink.Index_BT, VBLink.Index_VP);
    
	AD_Button *multiButton = (AD_Button*)[[(AD_ParallaxView*)[pageOB.mut_Arr_ParallaxView objectAtIndex:VBLink.Index_VP] AD_ButtonSets] objectAtIndex:VBLink.Index_BT];
	
    [self buttonAction:multiButton];
}

- (void)AddParaImage:(int)Index_PV
             scrollV:(UIScrollView *)scrollV
{   
	for(AD_Image *img in [[pageOB.mut_Arr_ViewPorts objectAtIndex:Index_PV] AD_ImageSet])
	{
        UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfFile:img.imagePath]]];
        NSArray *imgCondition = [img.Condition componentsSeparatedByString:@"=="];
        imageV.frame = CGRectMake(img.top, img.left, img.width, img.height);
        imageV.tag = img.Movement;
        
        [imageV setHidden:[self setCondition:imgCondition]];
		[scrollV addSubview:imageV]; //Tested memory removed
	}
}


- (void)AddParaView{
    int i= 0;
    for (AD_ParallaxView *pv in pageOB.mut_Arr_ParallaxView)
    {
        UIScrollView *scrollV = [[UIScrollView alloc]init];
        scrollV.contentSize = CGSizeMake(pv.Width, pv.Height);
        
        int scrollVWidth = 768;
        int scrollVHeight = 1024;
        
        if(pv.Width < scrollVWidth)
            scrollVWidth = pv.Width;
        if(pv.Height < scrollVHeight)
            scrollVHeight = pv.Height;
        
        scrollV.frame = CGRectMake(pv.left, pv.top, scrollVWidth, scrollVHeight);
        scrollV.showsHorizontalScrollIndicator = NO;
        scrollV.showsVerticalScrollIndicator = NO;
        scrollV.scrollsToTop = NO;
        scrollV.scrollEnabled = YES;
        scrollV.delegate = self;
        scrollV.bounces = YES;
        
        [self AddButton:i++ scrollV:scrollV];
        [self.view addSubview:scrollV];
        [scrollV release];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return YES;
}

- (void)dealloc {
    [ImageBG release];
    [PLBTLinkSets release];
    [super dealloc];
}

@end
